-- Bussinessman
-- Created by chengb Aug/2/2016
-- 商人

-- 商人冈布奥的编号
local PRIEST_PET_ID = 18;

-- 莱迦镇道具商店的位置
local LAIJIA_SHOP_POS = 12;

-- 初始化
function initBussinessman(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local iconPath = getHeroIconPath(PRIEST_PET_ID);

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 还没有领取商人任务
            if not BraveVSDragonM.hasReceivedBussiness() then
                local function okFunc()
                    -- 接受商人任务
                    if DungeonActionM.go("receive_bussiness", self.gridData:getPos()) then
                        alert(getLocStr("received_bussiness"));
                        local bonus = FormulaM.invoke("CALC_RECEIVE_BUSSINESS_BONUS");
                        self:playBonusListEffect(bonus, false);
                    end
                end

                self:openBuildingView2(okFunc, nil, buildingInfo["view_text"],
                    getLocStr("btn_text_leave"), buildingInfo["dialog"], iconPath, true);

                return;
            end

            -- 音效
            AudioM.playFx("ui_open1");

            local elementDbase = DungeonM.queryElement(self.gridData.element, "dbase");
            local function okFunc()
                local classId = elementDbase["submit_item"];
                if type(classId) == 'number' then
                    if ItemM.getAmount(ME.user, classId) <= 0 then
                        -- 缺少货物
                        alert(getLocStr("lack_submit_item"));
                        return;
                    end
                end

                DungeonActionM.go("brave_building_bonus", self.gridData:getPos());

                -- 如果是莱迦镇的商人，商人消失后，需要在商店播放一个光效
                if elementDbase["mel_kid"] ~= 1 then
                    local uiGrid = UIDungeonMgr.getCurLevel():getAllGrids()[LAIJIA_SHOP_POS];
                    uiGrid:playShopUnlockEffect();
                end
            end

            if elementDbase["mel_kid"] == 1 then
                -- 梅尔基德的商人
                if not BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
                    -- 未收到货物
                    self:openBuildingView2(okFunc, nil, nil, nil, nil, iconPath);
                else
                    self:openBuildingView(nil, nil, nil, buildingInfo["view_text2"], nil, iconPath);
                end
            else
                self:openBuildingView2(okFunc, nil, buildingInfo["view_text2"],
                    getLocStr("btn_text_leave"), buildingInfo["dialog2"], iconPath);
            end
        end
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setVisible(true);
    self.icon:setScale(1.2);
    self.item:setPositionY(-10);

    -- 呼吸
    self.needAnimation = true;

    if BraveVSDragonM.hasGotBuildingBonus(self.gridData.class) then
        -- 已经领取过奖励，格子隐藏
        self.icon:setVisible(false);
    else
        self.icon:setVisible(true);
    end
end
